﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using SenseTimeTracker.Properties;

namespace SenseTimeTracker.Misc
{
	public class EnumExtension
	{
		static public Dictionary<object, string> GetValues(Enum enumeration)
		{
			return GetValues(enumeration.GetType());
		}

		static public Dictionary<object, string> GetValues(Type type)
		{
			Dictionary<object, string> keyValuePair = new Dictionary<object, string>();
			foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Static | BindingFlags.Public))
			{
				string name = fieldInfo.Name;
				DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
				if (attributes.Length > 0)
				{
					name = Resources.ResourceManager.GetString(attributes[0].Description, Resources.Culture);
					if (name == null)
					{
						name = attributes[0].Description;
					}
				}
				keyValuePair.Add(fieldInfo.GetValue(null), name);
			}

			return keyValuePair;
		}

		static public int Level(object enumeration)
		{
			FieldInfo fieldInfo = enumeration.GetType().GetField(enumeration.ToString(), BindingFlags.Static | BindingFlags.Public);
	    ReportLevelAttribute[] attributes = (ReportLevelAttribute[])fieldInfo.GetCustomAttributes(typeof(ReportLevelAttribute), false);
	    if (attributes.Length > 0)
	    {
				return attributes[0].MenuLevel;
	    }

			return 0;
		}
	}

	[AttributeUsage(AttributeTargets.All)]
	public class ReportLevelAttribute : Attribute
	{
		private int menuLevel = 0;

		public ReportLevelAttribute(int menuLevel)
		{
			this.menuLevel = menuLevel;
		}

		public int MenuLevel
		{
			get { return menuLevel; }
			set { menuLevel = value; }
		}
	}

	[AttributeUsage(AttributeTargets.All)]
	public class DescriptionAttribute : Attribute
	{
		private string description;

		public DescriptionAttribute(string description)
		{
			this.description = description;
		}

		public string Description
		{
			get { return description; }
			set { description = value; }
		}
	}
}
